all files / src/ historical-date.ts

100% Statements 14/14
72.73% Branches 8/11
100% Functions 4/4
100% Lines 12/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42          1× 1397156×                           1× 1002× 1002×           1× 6000× 6000× 3000×   3000×     6000×       1×  
 
import { Calendar } from './calendar';
import { JulianDate } from "./julian-date";
import { GregorianDate } from "./gregorian-date";
 
export abstract class HistoricalDate {
    constructor(public readonly calendar: Calendar) { }
 
    abstract year: number | undefined;
    abstract month: number | undefined;
    abstract day: number | undefined;
    abstract readonly isLeapYear: boolean;
    abstract toGregorian(): GregorianDate;
    abstract toJulian(): JulianDate
 
    /**
     * Returns a new date which is moved by the specified number of days.
     */
    abstract addDays(days: number): HistoricalDate;
 
    toDate(): Date {
        let gregorian = this.toGregorian();
        return new Date(gregorian.year || 1, (gregorian.month || 1) - 1, (gregorian.day || 1));
    }
 
    /**
     * Checks whether this is the same date as another date.
     */
    equals(other: HistoricalDate) {
        let converted: HistoricalDate;
        if (this.calendar === 'gregorian') {
            converted = other.toGregorian();
        } else {
            converted = other.toJulian();
        }
 
        return this.year === converted.year &&
            this.month === converted.month &&
            this.day === converted.day;
    }
}